//
//  STMultiMealVC.m
//  YiYueHairdresser
//
//  Created by 路人甲 on 2018/11/2.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STMultiMealVC.h"
#import "SWMultiMealView.h"

@interface STMultiMealVC (){
    SWMultiMealView *multiMealView;
    STServiceInfoModel * serviceModel;
    NSArray * arrServiceType;
 
}

@end

@implementation STMultiMealVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"多项套餐";
    [self wr_setNavBarTintColor:[UIColor blackColor]];
    
    if(!serviceModel){
        serviceModel = [STServiceInfoModel new];
    }
    serviceModel.categoryId = 0;
    serviceModel.stylistId = [[YLUserInfo sharedInstance].stylistId longValue];
    
    [self loadMultiMealView];
    [self loadServiceType];
    if(self.serviceId){
        [self loadServiceSetInfo];
    }
        
}


//查询服务
-(void)loadServiceSetInfo{
    [STHairService getBarberServerInfoWithServiceId:self.serviceId completion:^(id result, NSError *error) {
        if (error) {
            [ZYHCommonMethod showError:error];
        }
        serviceModel = [STServiceInfoModel modelWithJSON:result];
        //pakageOptionDetails 分为单个
        [self splitUpPakageOption];
        multiMealView.serviceModel = serviceModel;
        [multiMealView reloadMultiTableView];
    }];
}
//查询类目
-(void)loadServiceType{
    [STHairService getBarberConditionWithPackageType:2 completion:^(id result, NSError *error) {
        if (error) {
            [ZYHCommonMethod showError:error];
        }
        arrServiceType= [NSArray modelArrayWithClass:[STServiceCategoryModel class] json:result];
        multiMealView.arrServiceType = arrServiceType;
    }];
}

//拆分数据
-(void)splitUpPakageOption{
    NSMutableArray * arrNewPackageOptions = [NSMutableArray arrayWithCapacity:0];
    for (STServiceOptionModel * optionModel in serviceModel.packageOptions) {
        for (STServiceOOModel * ooModel in optionModel.pakageOptionDetails) {
            STServiceOptionModel * oneModel = [STServiceOptionModel new];
            oneModel.categoryId = optionModel.categoryId;
            oneModel.optiontitle = optionModel.optiontitle;
            oneModel.optionId = optionModel.optionId;
            oneModel.optionname = optionModel.optionname;
            STServiceOOModel * newOoModel = [STServiceOOModel new];
            newOoModel.spcId =ooModel.spcId;
            newOoModel.optionvalue =ooModel.optionvalue;
            oneModel.pakageOptionDetails = (NSMutableArray<STServiceOOModel> *)[NSMutableArray arrayWithArray:@[newOoModel]];
            [arrNewPackageOptions addObject:oneModel];
        }
    }
    serviceModel.packageOptions = (NSMutableArray<STServiceOptionModel> *)arrNewPackageOptions;
}

- (void)loadMultiMealView {
    WeakSelf
    multiMealView = [[SWMultiMealView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    multiMealView.serviceModel = serviceModel;
    multiMealView.saveBtnAction = ^(id result) {
        [weakSelf actionSaveMulti:result];
    };
    [self.view addSubview:multiMealView];
}

-(void)actionSaveMulti:(STServiceInfoModel *)model{
    NSDictionary * oldDic = [model modelToJSONObject];
    NSMutableDictionary * dicParameter = [NSMutableDictionary dictionaryWithCapacity:0];
    NSArray * allKeys = oldDic.allKeys;
    for (NSString * key in allKeys) {
        id value = oldDic[key];
        if([value isKindOfClass:[NSNumber class]]){
            NSNumber * valueNum = value;
            if ([key isEqualToString:@"categoryId"] ||[key isEqualToString:@"duration"] || ![valueNum isEqualToNumber:@0]) {
                [dicParameter setObject:valueNum forKey:key];
            }
        }else{
            [dicParameter setObject:value forKey:key];
        }
    }
    
    NSMutableArray * arrNew = [NSMutableArray arrayWithCapacity:model.packageOptions.count];
    for(STServiceOptionModel * oldModel in model.packageOptions){
        STServiceOptionModel * newModel = [STServiceOptionModel new];
        STServiceOOModel * oomodel =oldModel.pakageOptionDetails.firstObject;
        if(oomodel){
            newModel.optionvalue = oomodel.optionvalue;
            if(oomodel.spcId){
                newModel.spcId = oomodel.spcId;
            }
        }
        newModel.categoryId = oldModel.categoryId;
        newModel.optionId = oldModel.optionId;
        newModel.optionname  = oldModel.optionname;
        newModel.optiontitle = oldModel.optiontitle;
        if(!newModel.optionvalue){
            newModel.optionvalue = @"";
        }
            
        NSMutableDictionary * dicNew =[NSMutableDictionary dictionaryWithDictionary:[newModel modelToJSONObject]];
//        NSNumber * spcId = ;
        if ([dicNew[@"spcId"] isEqualToNumber:@0]) {
            [dicNew removeObjectForKey:@"spcId"];
        }
        [arrNew addObject:dicNew];
    }
    
    [dicParameter setObject:@4 forKey:@"serviceType"];
    [dicParameter setObject:@"" forKey:@"decription"];
    [dicParameter setObject:arrNew forKey:@"packageOptions"];
    [STHairService saveBarberServerWithParameters:dicParameter completion:^(id result, NSError *error) {
        if(error){
            [ZYHCommonMethod showError:error];
        }else{
            [ZYHCommonMethod showSuccess:@"保存成功"];
        }
    }];
}

@end
